package Controllers;

import Bean.BeanStudent;
import Bean.BeanUser;
import Impl.OrderServiceImpl;
import Impl.StudentServiceImpl;
import Impl.UserServiceImpl;
import cart.ShoppingCart;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class StudentAddController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    StudentServiceImpl studentService;
	 @RequestMapping(value = "/StudentAdd", method = RequestMethod.POST)
	 protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 // TODO Auto-generated method stub
		 ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
		 studentService=(StudentServiceImpl)applicationContext.getBean("StudentServiceImpl");
		 try {
			 BeanStudent student = new BeanStudent();
			 student.setStudent_age(Integer.parseInt(request.getParameter("studentage")));
			 student.setStudent_name(request.getParameter("studentname"));
			 student.setUser_id((String)(request.getSession().getAttribute("userid")));
			 studentService.addStudent(student);
			 request.getRequestDispatcher("index/index.jsp").forward(request, response);
		 } catch (Exception e) {
			 // TODO Auto-generated catch block
			 request.getSession().setAttribute("errmsg", e.getMessage());
			 e.printStackTrace();
		 }
	 }
//		@ResponseBody
//		public void userRegister(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		 	ApplicationContext applicationContext=(ApplicationContext)request.getSession().getServletContext().getAttribute("ctx");
//	        studentService=(StudentServiceImpl)applicationContext.getBean("StudentServiceImpl");
//			JSONObject obj = new JSONObject();
//			BeanStudent student = new BeanStudent();
//		 	student.setStudent_age(Integer.parseInt(request.getParameter("studentage")));
//			student.setStudent_name(request.getParameter("studentname"));
//			student.setUser_id((String)(request.getSession().getAttribute("userid")));
//			studentService.addStudent(student);
//		 	//request.getRequestDispatcher("index/index.jsp").forward(request, response);
//		 	obj.put("status", "OK");
//			return obj;
//		}
}
